package com.ywy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywy.common.rest.Result;
import com.ywy.entity.UserMessage;
import com.ywy.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户消息 前端控制器
 * </p>
 *
 * @author ywy
 * @since 2021-12-24
 */
@RestController
@RequestMapping("/message")
public class UserMessageController extends BaseController {
    @Autowired
    private UserMessageService userMessageService;

    /**
     * 删除用户消息
     * @param id
     * @param all
     * @return
     */
    @PostMapping("/remove")
    public Result removeMsg(Integer id, @RequestParam(defaultValue = "false") boolean all) {
        // 只能删除自己的消息
        QueryWrapper<UserMessage> queryWrapper = new QueryWrapper<UserMessage>()
                .eq("to_user_id", getProfileId())
                .eq(!all, "id", id);
        boolean res = userMessageService.remove(queryWrapper);
        return res ? Result.success() : Result.failure("删除失败");
    }

    /**
     * 获取未读消息个数
     * @return
     */
    @PostMapping("/nums")
    public Result messageNums() {
        QueryWrapper<UserMessage> queryWrapper = new QueryWrapper<UserMessage>()
                .eq("to_user_id", getProfileId())
                .eq("status", 0);
        int count = userMessageService.count(queryWrapper);
        return Result.success(count);
    }
}
